/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.muni.fi.diplomka.datalayer.dao.AllianceMemberDao;
import cz.muni.fi.diplomka.datalayer.entity.AllianceMemberEntity;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;

/**
 *
 * @author Jakub Kutil
 */
public class AllianceMemberDaoImpl implements AllianceMemberDao {

    @Override
    public void addAllianceMember(AllianceMemberEntity allianceMemberEntity) {
        if (allianceMemberEntity == null) {
            throw new IllegalArgumentException("Alliance Member Entity can't be null.");
        }
        GraphDatabaseService gds = DbConnector.getDBConnection();
        ExecutionEngine engine = DbConnector.getEngine();


        try (Transaction t = gds.beginTx()) {

            Map<String, Object> params = new HashMap<>();

            params.put("playerId", allianceMemberEntity.getPlayerId());
            params.put("allianceId", allianceMemberEntity.getAllianceId());


            String query = "MATCH (Player:Player {id : {playerId}}),(Alliance:Alliance {id : {allianceId}}) "
                    + "CREATE (Player)-[r:BELONGS]->(Alliance)";
            engine.execute(query, params);
            t.success();
        }
    }

    @Override
    public void deleteAllianceMember(Long allianceMemberId) {
        if (allianceMemberId == null) {
            throw new IllegalArgumentException("Alliance Member ID can't be null.");
        }
        GraphDatabaseService gds = DbConnector.getDBConnection();
        ExecutionEngine engine = DbConnector.getEngine();


        try (Transaction t = gds.beginTx()) {

            Map<String, Object> params = new HashMap<>();

            params.put("allianceMemberId", allianceMemberId);

            String query = "MATCH (Player:Player {id : {allianceMemberId}})-[r:BELONGS]->(Alliance:Alliance)"
                    + "DELETE r";
            engine.execute(query, params);
            t.success();
        }
    }

    @Override
    public List<AllianceMemberEntity> getAllAllianceMembers(Long allianceId) {
        if (allianceId == null) {
            throw new IllegalArgumentException("Alliance Id can not be null.");
        }

        List<AllianceMemberEntity> allianceMembersList = new ArrayList<>();
        GraphDatabaseService gds = DbConnector.getDBConnection();
        ExecutionEngine engine = DbConnector.getEngine();


        try (Transaction t = gds.beginTx()) {

            Map<String, Object> params = new HashMap<>();

            params.put("allianceId", allianceId);

            String query = "MATCH (Player:Player)-[r:BELONGS]->(Alliance:Alliance {id : {allianceId}}) "
                    + "RETURN Player";
            ExecutionResult result = engine.execute(query, params);
            Iterator<Node> allianceMembers = result.columnAs("Player");
            while (allianceMembers.hasNext()) {
                Node node = allianceMembers.next();
                AllianceMemberEntity allianceMemberEntity = new AllianceMemberEntity();
                allianceMemberEntity.setAllianceId(allianceId);
                allianceMemberEntity.setPlayerId(Long.valueOf(node.getProperty("id").toString()));

                allianceMembersList.add(allianceMemberEntity);

            }
            t.success();
        }
        return allianceMembersList;
    }
}
